package com.by.service.purchase.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.by.common.bean.PageBean;
import com.by.common.exception.BusinessException;
import com.by.condition.purchase.PurchaseCondition;
import com.by.entity.purchase.Purchase;
import com.by.mapper.common.AttachmentMapper;
import com.by.mapper.purchase.PurchaseDetailMapper;
import com.by.mapper.purchase.PurchaseMapper;
import com.by.service.purchase.PurchaseService;
import com.by.utils.AssertsUtils;
import com.by.utils.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;
import java.util.UUID;

@Service
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> implements PurchaseService {


    /**
     * 当前系统采购申请最大编号
     */
    private String CURRENT_MAX_CODE;

    //编码前缀
    private final String CODE_PREFIX = "CGSQ";


    @Autowired
    PurchaseDetailMapper purchaseDetailMapper;

    @Autowired
    AttachmentMapper attachmentMapper;

    @Override
    public Purchase queryDetail(String id) {
        return baseMapper.queryById(id);
    }

    @Override
    public PageBean query(PurchaseCondition condition) {
        PageHelper.startPage(condition.getPage(), condition.getPageSize());
        List list = baseMapper.query(condition);

        PageInfo pageInfo = new PageInfo(list);
        return new PageBean(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getList());
    }

    @Transactional
    @Override
    public boolean save(Purchase entity) {
        //采购申请单id
        String id = UUID.randomUUID().toString();
        entity.setId(id);
        //根据是否询价  设置询价状态
        entity.setEnquiryStatus((byte) (entity.getIsenquiry() == 1 ? 1 : 0));
        entity.setCode(getNextCode());

        //保存采购申请单
        int flag = baseMapper.savePurchase(entity);
        if (flag == 0)
            return false;
        AssertsUtils.isNull(entity.getPurchaseDetails(), "采购申请单明细不能为空！");
        //保存明细
        int rs = purchaseDetailMapper.saveBatch(entity.getPurchaseDetails(), id);
        if (rs == 0)
            AssertsUtils.rollback("采购明细保存失败");
        //保存附件
        if (AssertsUtils.isNotNull(entity.getAttachments())) {
            rs = attachmentMapper.saveBatch(entity.getAttachments(), (byte) 1, id);
            if (rs == 0)
                AssertsUtils.rollback("附件保存失败");
        }

        //走到最后才是成功
        return true;
    }

    /**
     * 获取新的编号
     *
     * @return
     */
    String getNextCode() {

        //获取当前系统日期
        String ym = DateUtils.getSystemYM();  //202210

        //1. 先判断当前内存中 是否存在最大编码
        if (null == CURRENT_MAX_CODE) {
            //2.不存在  从数据库查询  当月最大编码
            CURRENT_MAX_CODE = baseMapper.queryMaxCode();
            //计算新的编码
            if (null == CURRENT_MAX_CODE) {
                //产生新的编码  存到内存
                CURRENT_MAX_CODE = CODE_PREFIX + ym + "0001";
                return CURRENT_MAX_CODE;
            }

        }

        //内存中存在着最大编码   CGSQ 202209 0223    CGSQ 202209 0224     CGSQ 202210 0001
        //获取当前编码的年月
        String curYm = CURRENT_MAX_CODE.substring(4, 10);
        //判断当前系统时间与现有编码curYm是否同月
        if (curYm.equals(ym)) {
            //获取后4位
            String num = CURRENT_MAX_CODE.substring(CURRENT_MAX_CODE.length() - 4);
            int newNum = Integer.parseInt(num);
            newNum++;
            CURRENT_MAX_CODE = CODE_PREFIX + ym + String.format("%04d", newNum);
        } else {
            //新的一月  编号归零
            CURRENT_MAX_CODE = CODE_PREFIX + ym + "0001";
        }
        return CURRENT_MAX_CODE;
    }

}
